import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_security_box_app/password_generator/rule_row.dart';

import 'logic.dart';

class PasswordGeneratorPage extends StatelessWidget {
  final PasswordGeneratorLogic logic = Get.put(PasswordGeneratorLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.close),
          onPressed: () {
            Get.back();
          },
        ),
        title: Text('密码生成器'),
        actions: _actions(context),
      ),
      body: _buildBody(context),
    );
  }

  Widget _buildBody(BuildContext context) {
    return Container(
      color: Color(0xfff0f0f0),
      padding: EdgeInsets.all(16),
      child: ListView(
        children: [
          _buildPasswordBox(),
        ]..addAll(_buildRuleSettings()),
      ),
    );
  }

  Widget _buildPasswordBox() {
    return Container(
      padding: EdgeInsets.fromLTRB(16, 8, 8, 8),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4), color: Colors.white),
      child: Row(
        children: [
          Expanded(
            child: Obx(() => Text(
                  logic.password,
                  style: TextStyle(fontSize: 18),
                )),
          ),
          IconButton(
            icon: Icon(Icons.refresh),
            onPressed: logic.generate,
          )
        ],
      ),
    );
  }

  List<Widget> _buildRuleSettings() {
    return [
      Row(children: [
        Text('长度'),
        Expanded(
          child: Obx(() => Slider(
                min: logic.minLength.toDouble(),
                max: logic.maxLength.toDouble(),
                value: logic.length.toDouble(),
                onChanged: (value) => logic.length.value = value.toInt(),
              )),
        ),
        Obx(() => Text(logic.length.toString()))
      ]),
      Obx(() => RuleRow('A-Z', logic.upperCaseLetter.value, (value) => logic.upperCaseLetter.value = value)),
      Obx(() => RuleRow('a-z', logic.lowerCaseLetter.value, (value) => logic.lowerCaseLetter.value = value)),
      Obx(() => RuleRow('0-9', logic.arabicNumerals.value, (value) => logic.arabicNumerals.value = value)),
      Obx(() => RuleRow(logic.specialSymbolString, logic.specialSymbol.value, (value) => logic.specialSymbol.value = value)),
    ];
  }

  List<Widget> _actions(BuildContext context) {
    return [IconButton(icon: Icon(Icons.done), onPressed: () {})];
  }
}
